<?php


namespace App\logic;


use App\Models\hospitalInformation;
use App\servers\wjy\EsServer;
use League\CommonMark\Extension\CommonMark\Parser\Inline\EscapableParser;

class ReservationLogic
{
    /**
     * 医院列表展示
     * @return mixed
     */
    public static function hospitalList(){
        $hospitalList=hospitalInformation::lists();
        return $hospitalList;
    }
    /**
     * 医院信息添加
     */
    public static function hospitalAdd($request)
    {
        $validated = $request->validate([
            'doctor_name' => 'required',
            'doctor_grade' => 'required',
            'doctor_Package_number' => 'required',
            'doctor_address' => 'required',
            'doctor_Package_bottom_price' => 'required',
            'doctor_Package_reservation_Number' => 'required',
        ], [
            'doctor_name.required' => '不能为空',
            'doctor_grade.required' => '不能为空',
            'doctor_Package_number.required' => '不能为空',
            'doctor_address.required' => '不能为空',
            'doctor_Package_bottom_price.required' => '不能为空',
            'doctor_Package_reservation_Number.required' => '不能为空',
        ]);
        $hospitalModel = hospitalInformation::create($validated);
        return $hospitalModel;
    }

    /**
     * 医院信息删除
     */
    public static function hospitalDel($hospitalId)
    {
        if (empty($hospitalId)) throw new \Exception('未接收到想要删除的房源信息');
        $executeDel = hospitalInformation::del('id', $hospitalId);
        return $executeDel;
    }

    /**
     * 医院信息查找
     */
    public static function hospitalSeek($query)
    {
        if (empty($query)) {
            return hospitalInformation::list();
        }
        $executeSeek = hospitalInformation::Seek('doctor_name', $query);
        return $executeSeek;
    }

    /**
     * 医院详情
     */
    public static function hospitalDetails($hospitalId)
    {
        if (empty($hospitalId)) {
            throw new \Exception('未接收到想要查看的医院信息');
        }
        $executeDetail = hospitalInformation::detail('id', $hospitalId);
        return $executeDetail;
    }

    /**
     * 医院修改
     */
    public static function hospitalUpdated($hospitalMessageUpdated)
    {
        unset($hospitalMessageUpdated['updated_at']);
        unset($hospitalMessageUpdated['created_at']);
        unset($hospitalMessageUpdated['deleted_at']);
        $fangID = $hospitalMessageUpdated['id'];
        $hospitalModel = hospitalInformation::change('id', $fangID, $hospitalMessageUpdated);
        return $hospitalModel;
    }
    /**
     * 医院信息存入ES
     */
    public static function PackageEstateEsCreate($realeEstateList){
        foreach($realeEstateList as $v){
            (new EsServer())->EsAdd('hospital',$v);
        }
        return true;
    }
    /**
     * 医院es搜索
     */
    public static function hospitalSearch($query)
    {
        if (empty($query)) {
            return hospitalInformation::list();
        }
        $executeEsSearch = new EsServer();
        $EsMessage['data'] = $executeEsSearch->searchEs('hospital', $query, 'doctor_name');
        return $EsMessage;
    }


}
